home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / f2c / src / proc.c < prev    next >
C/C++ Source or Header  |  1994-05-06  |  37KB  |  1,759 lines

  1. /****************************************************************
  2. Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27. #include "p1defs.h"
  28.  
  29. #define EXNULL (union Expression *)0
  30.  
  31. static void dobss Argdcl((void));
  32. static void docomleng Argdcl((void));
  33. static void docommon Argdcl((void));
  34. static void doentry Argdcl((struct Entrypoint*));
  35. static void epicode Argdcl((void));
  36. static int nextarg Argdcl((int));
  37. static void retval Argdcl((int));
  38.  
  39. static char Blank[] = BLANKCOMMON;
  40.  
  41.  static char *postfix[] = { "g", "h", "i",
  42. #ifdef TYQUAD
  43.                     "j",
  44. #endif
  45.                     "r", "d", "c", "z", "g", "h", "i" };
  46.  
  47.  chainp new_procs;
  48.  int prev_proc, proc_argchanges, proc_protochanges;
  49.  
  50.  void
  51. #ifdef KR_headers
  52. changedtype(q)
  53.     Namep q;
  54. #else
  55. changedtype(Namep q)
  56. #endif
  57. {
  58.     char buf[200];
  59.     int qtype, type1;
  60.     register Extsym *e;
  61.     Argtypes *at;
  62.  
  63.     if (q->vtypewarned)
  64.         return;
  65.     q->vtypewarned = 1;
  66.     qtype = q->vtype;
  67.     e = &extsymtab[q->vardesc.varno];
  68.     if (!(at = e->arginfo)) {
  69.         if (!e->exused)
  70.             return;
  71.         }
  72.     else if (at->changes & 2 && qtype != TYUNKNOWN && !at->defined)
  73.         proc_protochanges++;
  74.     type1 = e->extype;
  75.     if (type1 == TYUNKNOWN)
  76.         return;
  77.     if (qtype == TYUNKNOWN)
  78.         /* e.g.,
  79.             subroutine foo
  80.             end
  81.             external foo
  82.             call goo(foo)
  83.             end
  84.         */
  85.         return;
  86.     sprintf(buf, "%.90s: inconsistent declarations:\n\
  87.     here %s%s, previously %s%s.", q->fvarname, ftn_types[qtype],
  88.         qtype == TYSUBR ? "" : " function",
  89.         ftn_types[type1], type1 == TYSUBR ? "" : " function");
  90.     warn(buf);
  91.     }
  92.  
  93.  void
  94. #ifdef KR_headers
  95. unamstring(q, s)
  96.     register Addrp q;
  97.     register char *s;
  98. #else
  99. unamstring(register Addrp q, register char *s)
  100. #endif
  101. {
  102.     register int k;
  103.     register char *t;
  104.  
  105.     k = strlen(s);
  106.     if (k < IDENT_LEN) {
  107.         q->uname_tag = UNAM_IDENT;
  108.         t = q->user.ident;
  109.         }
  110.     else {
  111.         q->uname_tag = UNAM_CHARP;
  112.         q->user.Charp = t = mem(k+1, 0);
  113.         }
  114.     strcpy(t, s);
  115.     }
  116.  
  117.  static void
  118. fix_entry_returns(Void)    /* for multiple entry points */
  119. {
  120.     Addrp a;
  121.     int i;
  122.     struct Entrypoint *e;
  123.     Namep np;
  124.  
  125.     e = entries = (struct Entrypoint *)revchain((chainp)entries);
  126.     allargs = revchain(allargs);
  127.     if (!multitype)
  128.         return;
  129.  
  130.     /* TYLOGICAL should have been turned into TYLONG or TYSHORT by now */
  131.  
  132.     for(i = TYINT1; i <= TYLOGICAL; i++)
  133.         if (a = xretslot[i])
  134.             sprintf(a->user.ident, "(*ret_val).%s",
  135.                 postfix[i-TYINT1]);
  136.  
  137.     do {
  138.         np = e->enamep;
  139.         switch(np->vtype) {
  140.             case TYINT1:
  141.             case TYSHORT:
  142.             case TYLONG:
  143. #ifdef TYQUAD
  144.             case TYQUAD:
  145. #endif
  146.             case TYREAL:
  147.             case TYDREAL:
  148.             case TYCOMPLEX:
  149.             case TYDCOMPLEX:
  150.             case TYLOGICAL1:
  151.             case TYLOGICAL2:
  152.             case TYLOGICAL:
  153.                 np->vstg = STGARG;
  154.             }
  155.         }
  156.         while(e = e->entnextp);
  157.     }
  158.  
  159.  static void
  160. #ifdef KR_headers
  161. putentries(outfile)
  162.     FILE *outfile;
  163. #else
  164. putentries(FILE *outfile)
  165. #endif
  166.     /* put out wrappers for multiple entries */
  167. {
  168.     char base[IDENT_LEN];
  169.     struct Entrypoint *e;
  170.     Namep *A, *Ae, *Ae1, **Alp, *a, **a1, np;
  171.     chainp args, lengths;
  172.     int i, k, mt, nL, type;
  173.     extern char *dfltarg[], **dfltproc;
  174.  
  175.     e = entries;
  176.     if (!e->enamep) /* only possible with erroneous input */
  177.         return;
  178.     nL = (nallargs + nallchargs) * sizeof(Namep *);
  179.     A = (Namep *)ckalloc(nL + nallargs*sizeof(Namep **));
  180.     Ae = A + nallargs;
  181.     Alp = (Namep **)(Ae1 = Ae + nallchargs);
  182.     i = k = 0;
  183.     for(a1 = Alp, args = allargs; args; a1++, args = args->nextp) {
  184.         np = (Namep)args->datap;
  185.         if (np->vtype == TYCHAR && np->vclass != CLPROC)
  186.             *a1 = &Ae[i++];
  187.         }
  188.  
  189.     mt = multitype;
  190.     multitype = 0;
  191.     sprintf(base, "%s0_", e->enamep->cvarname);
  192.     do {
  193.         np = e->enamep;
  194.         lengths = length_comp(e, 0);
  195.         proctype = type = np->vtype;
  196.         if (protofile)
  197.             protowrite(protofile, type, np->cvarname, e, lengths);
  198.         nice_printf(outfile, "\n%s ", c_type_decl(type, 1));
  199.         nice_printf(outfile, "%s", np->cvarname);
  200.         if (!Ansi) {
  201.             listargs(outfile, e, 0, lengths);
  202.             nice_printf(outfile, "\n");
  203.             }
  204.             list_arg_types(outfile, e, lengths, 0, "\n");
  205.         nice_printf(outfile, "{\n");
  206.         frchain(&lengths);
  207.         next_tab(outfile);
  208.         if (mt)
  209.             nice_printf(outfile,
  210.                 "Multitype ret_val;\n%s(%d, &ret_val",
  211.                 base, k); /*)*/
  212.         else if (ISCOMPLEX(type))
  213.             nice_printf(outfile, "%s(%d,%s", base, k,
  214.                 xretslot[type]->user.ident); /*)*/
  215.         else if (type == TYCHAR)
  216.             nice_printf(outfile,
  217.                 "%s(%d, ret_val, ret_val_len", base, k); /*)*/
  218.         else
  219.             nice_printf(outfile, "return %s(%d", base, k); /*)*/
  220.         k++;
  221.         memset((char *)A, 0, nL);
  222.         for(args = e->arglist; args; args = args->nextp) {
  223.             np = (Namep)args->datap;
  224.             A[np->argno] = np;
  225.             if (np->vtype == TYCHAR && np->vclass != CLPROC)
  226.                 *Alp[np->argno] = np;
  227.             }
  228.         args = allargs;
  229.         for(a = A; a < Ae; a++, args = args->nextp)
  230.             nice_printf(outfile, ", %s", (np = *a)
  231.                 ? np->cvarname
  232.                 : ((Namep)args->datap)->vclass == CLPROC
  233.                 ? dfltproc[((Namep)args->datap)->vtype]
  234.                 : dfltarg[((Namep)args->datap)->vtype]);
  235.         for(; a < Ae1; a++)
  236.             if (np = *a)
  237.                 nice_printf(outfile, ", %s_len", np->fvarname);
  238.             else
  239.                 nice_printf(outfile, ", (ftnint)0");
  240.         nice_printf(outfile, /*(*/ ");\n");
  241.         if (mt) {
  242.             if (type == TYCOMPLEX)
  243.                 nice_printf(outfile,
  244.             "r_v->r = ret_val.c.r; r_v->i = ret_val.c.i;\n");
  245.             else if (type == TYDCOMPLEX)
  246.                 nice_printf(outfile,
  247.             "r_v->r = ret_val.z.r; r_v->i = ret_val.z.i;\n");
  248.             else if (type <= TYLOGICAL)
  249.                 nice_printf(outfile, "return ret_val.%s;\n",
  250.                     postfix[type-TYINT1]);
  251.             }
  252.         nice_printf(outfile, "}\n");
  253.         prev_tab(outfile);
  254.         }
  255.         while(e = e->entnextp);
  256.     free((char *)A);
  257.     }
  258.  
  259.  static void
  260. #ifdef KR_headers
  261. entry_goto(outfile)
  262.     FILE *outfile;
  263. #else
  264. entry_goto(FILE *outfile)
  265. #endif
  266. {
  267.     struct Entrypoint *e = entries;
  268.     int k = 0;
  269.  
  270.     nice_printf(outfile, "switch(n__) {\n");
  271.     next_tab(outfile);
  272.     while(e = e->entnextp)
  273.         nice_printf(outfile, "case %d: goto %s;\n", ++k,
  274.             user_label((long)(extsymtab - e->entryname - 1)));
  275.     nice_printf(outfile, "}\n\n");
  276.     prev_tab(outfile);
  277.     }
  278.  
  279. /* start a new procedure */
  280.  
  281.  void
  282. newproc(Void)
  283. {
  284.     if(parstate != OUTSIDE)
  285.     {
  286.         execerr("missing end statement", CNULL);
  287.         endproc();
  288.     }
  289.  
  290.     parstate = INSIDE;
  291.     procclass = CLMAIN;    /* default */
  292. }
  293.  
  294.  static void
  295. zap_changes(Void)
  296. {
  297.     register chainp cp;
  298.     register Argtypes *at;
  299.  
  300.     /* arrange to get correct count of prototypes that would
  301.        change by running f2c again */
  302.  
  303.     if (prev_proc && proc_argchanges)
  304.         proc_protochanges++;
  305.     prev_proc = proc_argchanges = 0;
  306.     for(cp = new_procs; cp; cp = cp->nextp)
  307.         if (at = ((Namep)cp->datap)->arginfo)
  308.             at->changes &= ~1;
  309.     frchain(&new_procs);
  310.     }
  311.  
  312. /* end of procedure. generate variables, epilogs, and prologs */
  313.  
  314.  void
  315. endproc(Void)
  316. {
  317.     struct Labelblock *lp;
  318.     Extsym *ext;
  319.  
  320.     if(parstate < INDATA)
  321.         enddcl();
  322.     if(ctlstack >= ctls)
  323.         err("DO loop or BLOCK IF not closed");
  324.     for(lp = labeltab ; lp < labtabend ; ++lp)
  325.         if(lp->stateno!=0 && lp->labdefined==NO)
  326.             errstr("missing statement label %s",
  327.                 convic(lp->stateno) );
  328.  
  329. /* Save copies of the common variables in extptr -> allextp */
  330.  
  331.     for (ext = extsymtab; ext < nextext; ext++)
  332.         if (ext -> extstg == STGCOMMON && ext -> extp) {
  333.             extern int usedefsforcommon;
  334.  
  335. /* Write out the abbreviations for common block reference */
  336.  
  337.             copy_data (ext -> extp);
  338.             if (usedefsforcommon) {
  339.                 wr_abbrevs (c_file, 1, ext -> extp);
  340.                 ext -> used_here = 1;
  341.                 }
  342.             else
  343.                 ext -> extp = CHNULL;
  344.  
  345.             }
  346.  
  347.     if (nentry > 1)
  348.         fix_entry_returns();
  349.     epicode();
  350.     donmlist();
  351.     dobss();
  352.     start_formatting ();
  353.     if (nentry > 1)
  354.         putentries(c_file);
  355.  
  356.     zap_changes();
  357.     procinit();    /* clean up for next procedure */
  358. }
  359.  
  360.  
  361.  
  362. /* End of declaration section of procedure.  Allocate storage. */
  363.  
  364.  void
  365. enddcl(Void)
  366. {
  367.     register struct Entrypoint *ep;
  368.     struct Entrypoint *ep0;
  369.     chainp cp;
  370.     extern char *err_proc;
  371.     static char comblks[] = "common blocks";
  372.  
  373.     err_proc = comblks;
  374.     docommon();
  375.  
  376. /* Now the hash table entries for fields of common blocks have STGCOMMON,
  377.    vdcldone, voffset, and varno.  And the common blocks themselves have
  378.    their full sizes in extleng. */
  379.  
  380.     err_proc = "equivalences";
  381.     doequiv();
  382.  
  383.     err_proc = comblks;
  384.     docomleng();
  385.  
  386. /* This implies that entry points in the declarations are buffered in
  387.    entries   but not written out */
  388.  
  389.     err_proc = "entries";
  390.     if (ep = ep0 = (struct Entrypoint *)revchain((chainp)entries)) {
  391.         /* entries could be 0 in case of an error */
  392.         do doentry(ep);
  393.             while(ep = ep->entnextp);
  394.         entries = (struct Entrypoint *)revchain((chainp)ep0);
  395.         }
  396.  
  397.     err_proc = 0;
  398.     parstate = INEXEC;
  399.     p1put(P1_PROCODE);
  400.     freetemps();
  401.     if (earlylabs) {
  402.         for(cp = earlylabs = revchain(earlylabs); cp; cp = cp->nextp)
  403.             p1_label((long)cp->datap);
  404.         frchain(&earlylabs);
  405.         }
  406.     p1_line_number(lineno); /* for files that start with a MAIN program */
  407.                 /* that starts with an executable statement */
  408. }
  409.  
  410. /* ROUTINES CALLED WHEN ENCOUNTERING ENTRY POINTS */
  411.  
  412. /* Main program or Block data */
  413.  
  414.  void
  415. #ifdef KR_headers
  416. startproc(progname, class)
  417.     Extsym *progname;
  418.     int class;
  419. #else
  420. startproc(Extsym *progname, int class)
  421. #endif
  422. {
  423.     register struct Entrypoint *p;
  424.  
  425.     p = ALLOC(Entrypoint);
  426.     if(class == CLMAIN) {
  427.         puthead(CNULL, CLMAIN);
  428.         if (progname)
  429.             strcpy (main_alias, progname->cextname);
  430.     } else
  431.         puthead(CNULL, CLBLOCK);
  432.     if(class == CLMAIN)
  433.         newentry( mkname(" MAIN"), 0 )->extinit = 1;
  434.     p->entryname = progname;
  435.     entries = p;
  436.  
  437.     procclass = class;
  438.     fprintf(diagfile, "   %s", (class==CLMAIN ? "MAIN" : "BLOCK DATA") );
  439.     if(progname) {
  440.         fprintf(diagfile, " %s", progname->fextname);
  441.         procname = progname->cextname;
  442.         }
  443.     fprintf(diagfile, ":\n");
  444.     fflush(diagfile);
  445. }
  446.  
  447. /* subroutine or function statement */
  448.  
  449.  Extsym *
  450. #ifdef KR_headers
  451. newentry(v, substmsg)
  452.     register Namep v;
  453.     int substmsg;
  454. #else
  455. newentry(register Namep v, int substmsg)
  456. #endif
  457. {
  458.     register Extsym *p;
  459.     char buf[128], badname[64];
  460.     static int nbad = 0;
  461.     static char already[] = "external name already used";
  462.  
  463.     p = mkext(v->fvarname, addunder(v->cvarname));
  464.  
  465.     if(p->extinit || ! ONEOF(p->extstg, M(STGUNKNOWN)|M(STGEXT)) )
  466.     {
  467.         sprintf(badname, "%s_bad%d", v->fvarname, ++nbad);
  468.         if (substmsg) {
  469.             sprintf(buf,"%s\n\tsubstituting \"%s\"",
  470.                 already, badname);
  471.             dclerr(buf, v);
  472.             }
  473.         else
  474.             dclerr(already, v);
  475.         p = mkext(v->fvarname, badname);
  476.     }
  477.     v->vstg = STGAUTO;
  478.     v->vprocclass = PTHISPROC;
  479.     v->vclass = CLPROC;
  480.     if (p->extstg == STGEXT)
  481.         prev_proc = 1;
  482.     else
  483.         p->extstg = STGEXT;
  484.     p->extinit = YES;
  485.     v->vardesc.varno = p - extsymtab;
  486.     return(p);
  487. }
  488.  
  489.  void
  490. #ifdef KR_headers
  491. entrypt(class, type, length, entry, args)
  492.     int class;
  493.     int type;
  494.     ftnint length;
  495.     Extsym *entry;
  496.     chainp args;
  497. #else
  498. entrypt(int class, int type, ftnint length, Extsym *entry, chainp args)
  499. #endif
  500. {
  501.     register Namep q;
  502.     register struct Entrypoint *p;
  503.  
  504.     if(class != CLENTRY)
  505.         puthead( procname = entry->cextname, class);
  506.     else
  507.         fprintf(diagfile, "       entry ");
  508.     fprintf(diagfile, "   %s:\n", entry->fextname);
  509.     fflush(diagfile);
  510.     q = mkname(entry->fextname);
  511.     if (type == TYSUBR)
  512.         q->vstg = STGEXT;
  513.  
  514.     type = lengtype(type, length);
  515.     if(class == CLPROC)
  516.     {
  517.         procclass = CLPROC;
  518.         proctype = type;
  519.         procleng = type == TYCHAR ? length : 0;
  520.     }
  521.  
  522.     p = ALLOC(Entrypoint);
  523.  
  524.     p->entnextp = entries;
  525.     entries = p;
  526.  
  527.     p->entryname = entry;
  528.     p->arglist = revchain(args);
  529.     p->enamep = q;
  530.  
  531.     if(class == CLENTRY)
  532.     {
  533.         class = CLPROC;
  534.         if(proctype == TYSUBR)
  535.             type = TYSUBR;
  536.     }
  537.  
  538.     q->vclass = class;
  539.     q->vprocclass = 0;
  540.     settype(q, type, length);
  541.     q->vprocclass = PTHISPROC;
  542.     /* hold all initial entry points till end of declarations */
  543.     if(parstate >= INDATA)
  544.         doentry(p);
  545. }
  546.  
  547. /* generate epilogs */
  548.  
  549. /* epicode -- write out the proper function return mechanism at the end of
  550.    the procedure declaration.  Handles multiple return value types, as
  551.    well as cooercion into the proper value */
  552.  
  553.  LOCAL void
  554. epicode(Void)
  555. {
  556.     extern int lastwasbranch;
  557.  
  558.     if(procclass==CLPROC)
  559.     {
  560.         if(proctype==TYSUBR)
  561.         {
  562.  
  563. /* Return a zero only when the alternate return mechanism has been
  564.    specified in the function header */
  565.  
  566.             if ((substars || Ansi) && lastwasbranch != YES)
  567.                 p1_subr_ret (ICON(0));
  568.         }
  569.         else if (!multitype && lastwasbranch != YES)
  570.             retval(proctype);
  571.     }
  572.     else if (procclass == CLMAIN && Ansi && lastwasbranch != YES)
  573.         p1_subr_ret (ICON(0));
  574.     lastwasbranch = NO;
  575. }
  576.  
  577.  
  578. /* generate code to return value of type  t */
  579.  
  580.  LOCAL void
  581. #ifdef KR_headers
  582. retval(t)
  583.     register int t;
  584. #else
  585. retval(register int t)
  586. #endif
  587. {
  588.     register Addrp p;
  589.  
  590.     switch(t)
  591.     {
  592.     case TYCHAR:
  593.     case TYCOMPLEX:
  594.     case TYDCOMPLEX:
  595.         break;
  596.  
  597.     case TYLOGICAL:
  598.         t = tylogical;
  599.     case TYINT1:
  600.     case TYADDR:
  601.     case TYSHORT:
  602.     case TYLONG:
  603. #ifdef TYQUAD
  604.     case TYQUAD:
  605. #endif
  606.     case TYREAL:
  607.     case TYDREAL:
  608.     case TYLOGICAL1:
  609.     case TYLOGICAL2:
  610.         p = (Addrp) cpexpr((expptr)retslot);
  611.         p->vtype = t;
  612.         p1_subr_ret (mkconv (t, fixtype((expptr)p)));
  613.         break;
  614.  
  615.     default:
  616.         badtype("retval", t);
  617.     }
  618. }
  619.  
  620.  
  621. /* Do parameter adjustments */
  622.  
  623.  void
  624. #ifdef KR_headers
  625. procode(outfile)
  626.     FILE *outfile;
  627. #else
  628. procode(FILE *outfile)
  629. #endif
  630. {
  631.     prolog(outfile, allargs);
  632.  
  633.     if (nentry > 1)
  634.         entry_goto(outfile);
  635.     }
  636.  
  637. /* Finish bound computations now that all variables are declared.
  638.  * This used to be in setbound(), but under -u the following incurred
  639.  * an erroneous error message:
  640.  *    subroutine foo(x,n)
  641.  *    real x(n)
  642.  *    integer n
  643.  */
  644.  
  645.  static void
  646. #ifdef KR_headers
  647. dim_finish(v)
  648.     Namep v;
  649. #else
  650. dim_finish(Namep v)
  651. #endif
  652. {
  653.     register struct Dimblock *p;
  654.     register expptr q;
  655.     register int i, nd;
  656.  
  657.     p = v->vdim;
  658.     v->vdimfinish = 0;
  659.     nd = p->ndim;
  660.     doin_setbound = 1;
  661.     for(i = 0; i < nd; i++)
  662.         if (q = p->dims[i].dimexpr) {
  663.             q = p->dims[i].dimexpr = make_int_expr(putx(fixtype(q)));
  664.             if (!ONEOF(q->headblock.vtype, MSKINT|MSKREAL))
  665.                 errstr("bad dimension type for %.70s",
  666.                     v->fvarname);
  667.             }
  668.     if (q = p->basexpr)
  669.         p->basexpr = make_int_expr(putx(fixtype(q)));
  670.     doin_setbound = 0;
  671.     }
  672.  
  673.  static void
  674. #ifdef KR_headers
  675. duparg(q)
  676.     Namep q;
  677. #else
  678. duparg(Namep q)
  679. #endif
  680. { errstr("duplicate argument %.80s", q->fvarname); }
  681.  
  682. /*
  683.    manipulate argument lists (allocate argument slot positions)
  684.  * keep track of return types and labels
  685.  */
  686.  
  687.  LOCAL void
  688. #ifdef KR_headers
  689. doentry(ep)
  690.     struct Entrypoint *ep;
  691. #else
  692. doentry(struct Entrypoint *ep)
  693. #endif
  694. {
  695.     register int type;
  696.     register Namep np;
  697.     chainp p, p1;
  698.     register Namep q;
  699.     Addrp rs;
  700.     int it, k;
  701.     extern char dflttype[26];
  702.     Extsym *entryname = ep->entryname;
  703.  
  704.     if (++nentry > 1)
  705.         p1_label((long)(extsymtab - entryname - 1));
  706.  
  707. /* The main program isn't allowed to have parameters, so any given
  708.    parameters are ignored */
  709.  
  710.     if(procclass == CLMAIN || procclass == CLBLOCK)
  711.         return;
  712.  
  713. /* So now we're working with something other than CLMAIN or CLBLOCK.
  714.    Determine the type of its return value. */
  715.  
  716.     impldcl( np = mkname(entryname->fextname) );
  717.     type = np->vtype;
  718.     proc_argchanges = prev_proc && type != entryname->extype;
  719.     entryname->extseen = 1;
  720.     if(proctype == TYUNKNOWN)
  721.         if( (proctype = type) == TYCHAR)
  722.             procleng = np->vleng ? np->vleng->constblock.Const.ci
  723.                          : (ftnint) (-1);
  724.  
  725.     if(proctype == TYCHAR)
  726.     {
  727.         if(type != TYCHAR)
  728.             err("noncharacter entry of character function");
  729.  
  730. /* Functions returning type   char   can only have multiple entries if all
  731.    entries return the same length */
  732.  
  733.         else if( (np->vleng ? np->vleng->constblock.Const.ci :
  734.             (ftnint) (-1)) != procleng)
  735.             err("mismatched character entry lengths");
  736.     }
  737.     else if(type == TYCHAR)
  738.         err("character entry of noncharacter function");
  739.     else if(type != proctype)
  740.         multitype = YES;
  741.     if(rtvlabel[type] == 0)
  742.         rtvlabel[type] = newlabel();
  743.     ep->typelabel = rtvlabel[type];
  744.  
  745.     if(type == TYCHAR)
  746.     {
  747.         if(chslot < 0)
  748.         {
  749.             chslot = nextarg(TYADDR);
  750.             chlgslot = nextarg(TYLENG);
  751.         }
  752.         np->vstg = STGARG;
  753.  
  754. /* Put a new argument in the function, one which will hold the result of
  755.    a character function.  This will have to be named sometime, probably in
  756.    mkarg(). */
  757.  
  758.         if(procleng < 0) {
  759.             np->vleng = (expptr) mkarg(TYLENG, chlgslot);
  760.             np->vleng->addrblock.uname_tag = UNAM_IDENT;
  761.             strcpy (np -> vleng -> addrblock.user.ident,
  762.                 new_func_length());
  763.             }
  764.         if (!xretslot[TYCHAR]) {
  765.             xretslot[TYCHAR] = rs =
  766.                 autovar(0, type, ISCONST(np->vleng)
  767.                     ? np->vleng : ICON(0), "");
  768.             strcpy(rs->user.ident, "ret_val");
  769.             }
  770.     }
  771.  
  772. /* Handle a   complex   return type -- declare a new parameter (pointer to
  773.    a complex value) */
  774.  
  775.     else if( ISCOMPLEX(type) ) {
  776.         if (!xretslot[type])
  777.             xretslot[type] =
  778.                 autovar(0, type, EXNULL, " ret_val");
  779.                 /* the blank is for use in out_addr */
  780.         np->vstg = STGARG;
  781.         if(cxslot < 0)
  782.             cxslot = nextarg(TYADDR);
  783.         }
  784.     else if (type != TYSUBR) {
  785.         if (type == TYUNKNOWN) {
  786.             dclerr("untyped function", np);
  787.             proctype = type = np->vtype =
  788.                 dflttype[letter(np->fvarname[0])];
  789.             }
  790.         if (!xretslot[type])
  791.             xretslot[type] = retslot =
  792.                 autovar(1, type, EXNULL, " ret_val");
  793.                 /* the blank is for use in out_addr */
  794.         np->vstg = STGAUTO;
  795.         }
  796.  
  797.     for(p = ep->arglist ; p ; p = p->nextp)
  798.         if(! (( q = (Namep) (p->datap) )->vknownarg) ) {
  799.             q->vknownarg = 1;
  800.             q->vardesc.varno = nextarg(TYADDR);
  801.             allargs = mkchain((char *)q, allargs);
  802.             q->argno = nallargs++;
  803.             }
  804.         else if (nentry == 1)
  805.             duparg(q);
  806.         else for(p1 = ep->arglist ; p1 != p; p1 = p1->nextp)
  807.             if ((Namep)p1->datap == q)
  808.                 duparg(q);
  809.  
  810.     k = 0;
  811.     for(p = ep->arglist ; p ; p = p->nextp) {
  812.         if(! (( q = (Namep) (p->datap) )->vdcldone) )
  813.             {
  814.             impldcl(q);
  815.             q->vdcldone = YES;
  816.             if(q->vtype == TYCHAR)
  817.                 {
  818.  
  819. /* If we don't know the length of a char*(*) (i.e. a string), we must add
  820.    in this additional length argument. */
  821.  
  822.                 ++nallchargs;
  823.                 if (q->vclass == CLPROC)
  824.                     nallchargs--;
  825.                 else if (q->vleng == NULL) {
  826.                     /* character*(*) */
  827.                     q->vleng = (expptr)
  828.                         mkarg(TYLENG, nextarg(TYLENG) );
  829.                     unamstring((Addrp)q->vleng,
  830.                         new_arg_length(q));
  831.                     }
  832.                 }
  833.             }
  834.         if (q->vdimfinish)
  835.             dim_finish(q);
  836.         if (q->vtype == TYCHAR && q->vclass != CLPROC)
  837.             k++;
  838.         }
  839.  
  840.     if (entryname->extype != type)
  841.         changedtype(np);
  842.  
  843.     /* save information for checking consistency of arg lists */
  844.  
  845.     it = infertypes;
  846.     if (entryname->exproto)
  847.         infertypes = 1;
  848.     save_argtypes(ep->arglist, &entryname->arginfo, &np->arginfo,
  849.             0, np->fvarname, STGEXT, k, np->vtype, 2);
  850.     infertypes = it;
  851. }
  852.  
  853.  
  854.  
  855.  LOCAL int
  856. #ifdef KR_headers
  857. nextarg(type)
  858.     int type;
  859. #else
  860. nextarg(int type)
  861. #endif
  862. {
  863.     type = type;    /* shut up warning */
  864.     return(lastargslot++);
  865.     }
  866.  
  867.  LOCAL void
  868. #ifdef KR_headers
  869. dim_check(q)
  870.     Namep q;
  871. #else
  872. dim_check(Namep q)
  873. #endif
  874. {
  875.     register struct Dimblock *vdim = q->vdim;
  876.  
  877.     if(!vdim->nelt || !ISICON(vdim->nelt))
  878.         dclerr("adjustable dimension on non-argument", q);
  879.     else if (vdim->nelt->constblock.Const.ci <= 0)
  880.         dclerr("nonpositive dimension", q);
  881.     }
  882.  
  883.  LOCAL void
  884. dobss(Void)
  885. {
  886.     register struct Hashentry *p;
  887.     register Namep q;
  888.     int qstg, qclass, qtype;
  889.     Extsym *e;
  890.  
  891.     for(p = hashtab ; p<lasthash ; ++p)
  892.         if(q = p->varp)
  893.         {
  894.             qstg = q->vstg;
  895.             qtype = q->vtype;
  896.             qclass = q->vclass;
  897.  
  898.             if( (qclass==CLUNKNOWN && qstg!=STGARG) ||
  899.                 (qclass==CLVAR && qstg==STGUNKNOWN) ) {
  900.                 if (!(q->vis_assigned | q->vimpldovar))
  901.                     warn1("local variable %s never used",
  902.                         q->fvarname);
  903.                 }
  904.             else if(qclass==CLVAR && qstg==STGBSS)
  905.             { ; }
  906.  
  907. /* Give external procedures the proper storage class */
  908.  
  909.             else if(qclass==CLPROC && q->vprocclass==PEXTERNAL
  910.                     && qstg!=STGARG) {
  911.                 e = mkext(q->fvarname,addunder(q->cvarname));
  912.                 e->extstg = STGEXT;
  913.                 q->vardesc.varno = e - extsymtab;
  914.                 if (e->extype != qtype)
  915.                     changedtype(q);
  916.                 }
  917.             if(qclass==CLVAR) {
  918.                 if (qstg != STGARG && q->vdim)
  919.                 dim_check(q);
  920.             } /* if qclass == CLVAR */
  921.         }
  922.  
  923. }
  924.  
  925.  
  926.  void
  927. donmlist(Void)
  928. {
  929.     register struct Hashentry *p;
  930.     register Namep q;
  931.  
  932.     for(p=hashtab; p<lasthash; ++p)
  933.         if( (q = p->varp) && q->vclass==CLNAMELIST)
  934.             namelist(q);
  935. }
  936.  
  937.  
  938. /* iarrlen -- Returns the size of the array in bytes, or -1 */
  939.  
  940.  ftnint
  941. #ifdef KR_headers
  942. iarrlen(q)
  943.     register Namep q;
  944. #else
  945. iarrlen(register Namep q)
  946. #endif
  947. {
  948.     ftnint leng;
  949.  
  950.     leng = typesize[q->vtype];
  951.     if(leng <= 0)
  952.         return(-1);
  953.     if(q->vdim)
  954.         if( ISICON(q->vdim->nelt) )
  955.             leng *= q->vdim->nelt->constblock.Const.ci;
  956.         else    return(-1);
  957.     if(q->vleng)
  958.         if( ISICON(q->vleng) )
  959.             leng *= q->vleng->constblock.Const.ci;
  960.         else return(-1);
  961.     return(leng);
  962. }
  963.  
  964.  void
  965. #ifdef KR_headers
  966. namelist(np)
  967.     Namep np;
  968. #else
  969. namelist(Namep np)
  970. #endif
  971. {
  972.     register chainp q;
  973.     register Namep v;
  974.     int y;
  975.  
  976.     if (!np->visused)
  977.         return;
  978.     y = 0;
  979.  
  980.     for(q = np->varxptr.namelist ; q ; q = q->nextp)
  981.     {
  982.         vardcl( v = (Namep) (q->datap) );
  983.         if( !ONEOF(v->vstg, MSKSTATIC) )
  984.             dclerr("may not appear in namelist", v);
  985.         else {
  986.             v->vnamelist = 1;
  987.             v->visused = 1;
  988.             v->vsave = 1;
  989.             y = 1;
  990.             }
  991.     np->visused = y;
  992.     }
  993. }
  994.  
  995. /* docommon -- called at the end of procedure declarations, before
  996.    equivalences and the procedure body */
  997.  
  998.  LOCAL void
  999. docommon(Void)
  1000. {
  1001.     register Extsym *extptr;
  1002.     register chainp q, q1;
  1003.     struct Dimblock *t;
  1004.     expptr neltp;
  1005.     register Namep comvar;
  1006.     ftnint size;
  1007.     int i, k, pref, type;
  1008.     extern int type_pref[];
  1009.  
  1010.     for(extptr = extsymtab ; extptr<nextext ; ++extptr)
  1011.     if (extptr->extstg == STGCOMMON && (q = extptr->extp)) {
  1012.  
  1013. /* If a common declaration also had a list of variables ... */
  1014.  
  1015.         q = extptr->extp = revchain(q);
  1016.         pref = 1;
  1017.         for(k = TYCHAR; q ; q = q->nextp)
  1018.         {
  1019.         comvar = (Namep) (q->datap);
  1020.  
  1021.         if(comvar->vdcldone == NO)
  1022.             vardcl(comvar);
  1023.         type = comvar->vtype;
  1024.         if (pref < type_pref[type])
  1025.             pref = type_pref[k = type];
  1026.         if(extptr->extleng % typealign[type] != 0) {
  1027.             dclerr("common alignment", comvar);
  1028.             --nerr; /* don't give bad return code for this */
  1029. #if 0
  1030.             extptr->extleng = roundup(extptr->extleng, typealign[type]);
  1031. #endif
  1032.         } /* if extptr -> extleng % */
  1033.  
  1034. /* Set the offset into the common block */
  1035.  
  1036.         comvar->voffset = extptr->extleng;
  1037.         comvar->vardesc.varno = extptr - extsymtab;
  1038.         if(type == TYCHAR)
  1039.             size = comvar->vleng->constblock.Const.ci;
  1040.         else
  1041.             size = typesize[type];
  1042.         if(t = comvar->vdim)
  1043.             if( (neltp = t->nelt) && ISCONST(neltp) )
  1044.             size *= neltp->constblock.Const.ci;
  1045.             else
  1046.             dclerr("adjustable array in common", comvar);
  1047.  
  1048. /* Adjust the length of the common block so far */
  1049.  
  1050.         extptr->extleng += size;
  1051.         } /* for */
  1052.  
  1053.         extptr->extype = k;
  1054.  
  1055. /* Determine curno and, if new, save this identifier chain */
  1056.  
  1057.         q1 = extptr->extp;
  1058.         for (q = extptr->allextp, i = 0; q; i++, q = q->nextp)
  1059.         if (struct_eq((chainp)q->datap, q1))
  1060.             break;
  1061.         if (q)
  1062.         extptr->curno = extptr->maxno - i;
  1063.         else {
  1064.         extptr->curno = ++extptr->maxno;
  1065.         extptr->allextp = mkchain((char *)extptr->extp,
  1066.                         extptr->allextp);
  1067.         }
  1068.     } /* if extptr -> extstg == STGCOMMON */
  1069.  
  1070. /* Now the hash table entries have STGCOMMON, vdcldone, voffset, and
  1071.    varno.  And the common block itself has its full size in extleng. */
  1072.  
  1073. } /* docommon */
  1074.  
  1075.  
  1076. /* copy_data -- copy the Namep entries so they are available even after
  1077.    the hash table is empty */
  1078.  
  1079.  void
  1080. #ifdef KR_headers
  1081. copy_data(list)
  1082.     chainp list;
  1083. #else
  1084. copy_data(chainp list)
  1085. #endif
  1086. {
  1087.     for (; list; list = list -> nextp) {
  1088.     Namep namep = ALLOC (Nameblock);
  1089.     int size, nd, i;
  1090.     struct Dimblock *dp;
  1091.  
  1092.     cpn(sizeof(struct Nameblock), list->datap, (char *)namep);
  1093.     namep->fvarname = strcpy(gmem(strlen(namep->fvarname)+1,0),
  1094.         namep->fvarname);
  1095.     namep->cvarname = strcmp(namep->fvarname, namep->cvarname)
  1096.         ? strcpy(gmem(strlen(namep->cvarname)+1,0), namep->cvarname)
  1097.         : namep->fvarname;
  1098.     if (namep -> vleng)
  1099.         namep -> vleng = (expptr) cpexpr (namep -> vleng);
  1100.     if (namep -> vdim) {
  1101.         nd = namep -> vdim -> ndim;
  1102.         size = sizeof(int) + (3 + 2 * nd) * sizeof (expptr);
  1103.         dp = (struct Dimblock *) ckalloc (size);
  1104.         cpn(size, (char *)namep->vdim, (char *)dp);
  1105.         namep -> vdim = dp;
  1106.         dp->nelt = (expptr)cpexpr(dp->nelt);
  1107.         for (i = 0; i < nd; i++) {
  1108.         dp -> dims[i].dimsize = (expptr) cpexpr (dp -> dims[i].dimsize);
  1109.         } /* for */
  1110.     } /* if */
  1111.     list -> datap = (char *) namep;
  1112.     } /* for */
  1113. } /* copy_data */
  1114.  
  1115.  
  1116.  
  1117.  LOCAL void
  1118. docomleng(Void)
  1119. {
  1120.     register Extsym *p;
  1121.  
  1122.     for(p = extsymtab ; p < nextext ; ++p)
  1123.         if(p->extstg == STGCOMMON)
  1124.         {
  1125.             if(p->maxleng!=0 && p->extleng!=0 && p->maxleng!=p->extleng
  1126.                 && strcmp(Blank, p->cextname) )
  1127.                 warn1("incompatible lengths for common block %.60s",
  1128.                     p->fextname);
  1129.             if(p->maxleng < p->extleng)
  1130.                 p->maxleng = p->extleng;
  1131.             p->extleng = 0;
  1132.         }
  1133. }
  1134.  
  1135.  
  1136. /* ROUTINES DEALING WITH AUTOMATIC AND TEMPORARY STORAGE */
  1137.  
  1138.  void
  1139. #ifdef KR_headers
  1140. frtemp(p)
  1141.     Addrp p;
  1142. #else
  1143. frtemp(Addrp p)
  1144. #endif
  1145. {
  1146.     /* put block on chain of temps to be reclaimed */
  1147.     holdtemps = mkchain((char *)p, holdtemps);
  1148. }
  1149.  
  1150.  void
  1151. freetemps(Void)
  1152. {
  1153.     register chainp p, p1;
  1154.     register Addrp q;
  1155.     register int t;
  1156.  
  1157.     p1 = holdtemps;
  1158.     while(p = p1) {
  1159.         q = (Addrp)p->datap;
  1160.         t = q->vtype;
  1161.         if (t == TYCHAR && q->varleng != 0) {
  1162.             /* restore clobbered character string lengths */
  1163.             frexpr(q->vleng);
  1164.             q->vleng = ICON(q->varleng);
  1165.             }
  1166.         p1 = p->nextp;
  1167.         p->nextp = templist[t];
  1168.         templist[t] = p;
  1169.         }
  1170.     holdtemps = 0;
  1171.     }
  1172.  
  1173. /* allocate an automatic variable slot for each of   nelt   variables */
  1174.  
  1175.  Addrp
  1176. #ifdef KR_headers
  1177. autovar(nelt0, t, lengp, name)
  1178.     register int nelt0;
  1179.     register int t;
  1180.     expptr lengp;
  1181.     char *name;
  1182. #else
  1183. autovar(register int nelt0, register int t, expptr lengp, char *name)
  1184. #endif
  1185. {
  1186.     ftnint leng;
  1187.     register Addrp q;
  1188.     register int nelt = nelt0 > 0 ? nelt0 : 1;
  1189.     extern char *av_pfix[];
  1190.  
  1191.     if(t == TYCHAR)
  1192.         if( ISICON(lengp) )
  1193.             leng = lengp->constblock.Const.ci;
  1194.         else    {
  1195.             Fatal("automatic variable of nonconstant length");
  1196.         }
  1197.     else
  1198.         leng = typesize[t];
  1199.  
  1200.     q = ALLOC(Addrblock);
  1201.     q->tag = TADDR;
  1202.     q->vtype = t;
  1203.     if(t == TYCHAR)
  1204.     {
  1205.         q->vleng = ICON(leng);
  1206.         q->varleng = leng;
  1207.     }
  1208.     q->vstg = STGAUTO;
  1209.     q->ntempelt = nelt;
  1210.     q->isarray = (nelt > 1);
  1211.     q->memoffset = ICON(0);
  1212.  
  1213.     /* kludge for nls so we can have ret_val rather than ret_val_4 */
  1214.     if (*name == ' ')
  1215.         unamstring(q, name);
  1216.     else {
  1217.         q->uname_tag = UNAM_IDENT;
  1218.         temp_name(av_pfix[t], ++autonum[t], q->user.ident);
  1219.         }
  1220.     if (nelt0 > 0)
  1221.         declare_new_addr (q);
  1222.     return(q);
  1223. }
  1224.  
  1225.  
  1226. /* Returns a temporary of the appropriate type.  Will reuse existing
  1227.    temporaries when possible */
  1228.  
  1229.  Addrp
  1230. #ifdef KR_headers
  1231. mktmpn(nelt, type, lengp)
  1232.     int nelt;
  1233.     register int type;
  1234.     expptr lengp;
  1235. #else
  1236. mktmpn(int nelt, register int type, expptr lengp)
  1237. #endif
  1238. {
  1239.     ftnint leng;
  1240.     chainp p, oldp;
  1241.     register Addrp q;
  1242.  
  1243.     if(type==TYUNKNOWN || type==TYERROR)
  1244.         badtype("mktmpn", type);
  1245.  
  1246.     if(type==TYCHAR)
  1247.         if(lengp && ISICON(lengp) )
  1248.             leng = lengp->constblock.Const.ci;
  1249.         else    {
  1250.             err("adjustable length");
  1251.             return( (Addrp) errnode() );
  1252.         }
  1253.     else if (type > TYCHAR || type < TYADDR) {
  1254.         erri("mktmpn: unexpected type %d", type);
  1255.         exit(1);
  1256.         }
  1257. /*
  1258.  * if a temporary of appropriate shape is on the templist,
  1259.  * remove it from the list and return it
  1260.  */
  1261.     for(oldp=CHNULL, p=templist[type];  p  ;  oldp=p, p=p->nextp)
  1262.     {
  1263.         q = (Addrp) (p->datap);
  1264.         if(q->ntempelt==nelt &&
  1265.             (type!=TYCHAR || q->vleng->constblock.Const.ci==leng) )
  1266.         {
  1267.             if(oldp)
  1268.                 oldp->nextp = p->nextp;
  1269.             else
  1270.                 templist[type] = p->nextp;
  1271.             free( (charptr) p);
  1272.             return(q);
  1273.         }
  1274.     }
  1275.     q = autovar(nelt, type, lengp, "");
  1276.     return(q);
  1277. }
  1278.  
  1279.  
  1280.  
  1281.  
  1282. /* mktmp -- create new local variable; call it something like   name
  1283.    lengp   is taken directly, not copied */
  1284.  
  1285.  Addrp
  1286. #ifdef KR_headers
  1287. mktmp(type, lengp)
  1288.     int type;
  1289.     expptr lengp;
  1290. #else
  1291. mktmp(int type, expptr lengp)
  1292. #endif
  1293. {
  1294.     Addrp rv;
  1295.     /* arrange for temporaries to be recycled */
  1296.     /* at the end of this statement... */
  1297.     rv = mktmpn(1,type,lengp);
  1298.     frtemp((Addrp)cpexpr((expptr)rv));
  1299.     return rv;
  1300. }
  1301.  
  1302. /* mktmp0 omits frtemp() */
  1303.  Addrp
  1304. #ifdef KR_headers
  1305. mktmp0(type, lengp)
  1306.     int type;
  1307.     expptr lengp;
  1308. #else
  1309. mktmp0(int type, expptr lengp)
  1310. #endif
  1311. {
  1312.     Addrp rv;
  1313.     /* arrange for temporaries to be recycled */
  1314.     /* when this Addrp is freed */
  1315.     rv = mktmpn(1,type,lengp);
  1316.     rv->istemp = YES;
  1317.     return rv;
  1318. }
  1319.  
  1320. /* VARIOUS ROUTINES FOR PROCESSING DECLARATIONS */
  1321.  
  1322. /* comblock -- Declare a new common block.  Input parameters name the block;
  1323.    s   will be NULL if the block is unnamed */
  1324.  
  1325.  Extsym *
  1326. #ifdef KR_headers
  1327. comblock(s)
  1328.     register char *s;
  1329. #else
  1330. comblock(register char *s)
  1331. #endif
  1332. {
  1333.     Extsym *p;
  1334.     register char *t;
  1335.     register int c, i;
  1336.     char cbuf[256], *s0;
  1337.  
  1338. /* Give the unnamed common block a unique name */
  1339.  
  1340.     if(*s == 0)
  1341.         p = mkext1(s0 = Blank, Blank);
  1342.     else {
  1343.         s0 = s;
  1344.         t = cbuf;
  1345.         for(i = 0; c = *t = *s++; t++)
  1346.             if (c == '_')
  1347.                 i = 1;
  1348.         if (i)
  1349.             *t++ = '_';
  1350.         t[0] = '_';
  1351.         t[1] = 0;
  1352.         p = mkext1(s0,cbuf);
  1353.         }
  1354.     if(p->extstg == STGUNKNOWN)
  1355.         p->extstg = STGCOMMON;
  1356.     else if(p->extstg != STGCOMMON)
  1357.     {
  1358.         errstr("%.52s cannot be a common block: it is a subprogram.",
  1359.             s0);
  1360.         return(0);
  1361.     }
  1362.  
  1363.     return( p );
  1364. }
  1365.  
  1366.  
  1367. /* incomm -- add a new variable to a common declaration */
  1368.  
  1369.  void
  1370. #ifdef KR_headers
  1371. incomm(c, v)
  1372.     Extsym *c;
  1373.     Namep v;
  1374. #else
  1375. incomm(Extsym *c, Namep v)
  1376. #endif
  1377. {
  1378.     if (!c)
  1379.         return;
  1380.     if(v->vstg != STGUNKNOWN && !v->vimplstg)
  1381.         dclerr(v->vstg == STGARG
  1382.             ? "dummy arguments cannot be in common"
  1383.             : "incompatible common declaration", v);
  1384.     else
  1385.     {
  1386.         v->vstg = STGCOMMON;
  1387.         c->extp = mkchain((char *)v, c->extp);
  1388.     }
  1389. }
  1390.  
  1391.  
  1392.  
  1393.  
  1394. /* settype -- set the type or storage class of a Namep object.  If
  1395.    v -> vstg == STGUNKNOWN && type < 0,   attempt to reset vstg to be
  1396.    -type.  This function will not change any earlier definitions in   v,
  1397.    in will only attempt to fill out more information give the other params */
  1398.  
  1399.  void
  1400. #ifdef KR_headers
  1401. settype(v, type, length)
  1402.     register Namep v;
  1403.     register int type;
  1404.     register ftnint length;
  1405. #else
  1406. settype(register Namep v, register int type, register ftnint length)
  1407. #endif
  1408. {
  1409.     int type1;
  1410.  
  1411.     if(type == TYUNKNOWN)
  1412.         return;
  1413.  
  1414.     if(type==TYSUBR && v->vtype!=TYUNKNOWN && v->vstg==STGARG)
  1415.     {
  1416.         v->vtype = TYSUBR;
  1417.         frexpr(v->vleng);
  1418.         v->vleng = 0;
  1419.         v->vimpltype = 0;
  1420.     }
  1421.     else if(type < 0)    /* storage class set */
  1422.     {
  1423.         if(v->vstg == STGUNKNOWN)
  1424.             v->vstg = - type;
  1425.         else if(v->vstg != -type)
  1426.             dclerr("incompatible storage declarations", v);
  1427.     }
  1428.     else if(v->vtype == TYUNKNOWN || v->vimpltype && v->vtype != type)
  1429.     {
  1430.         if( (v->vtype = lengtype(type, length))==TYCHAR )
  1431.             if (length>=0)
  1432.                 v->vleng = ICON(length);
  1433.             else if (parstate >= INDATA)
  1434.                 v->vleng = ICON(1);    /* avoid a memory fault */
  1435.         v->vimpltype = 0;
  1436.  
  1437.         if (v->vclass == CLPROC) {
  1438.             if (v->vstg == STGEXT
  1439.              && (type1 = extsymtab[v->vardesc.varno].extype)
  1440.              &&  type1 != v->vtype)
  1441.                 changedtype(v);
  1442.             else if (v->vprocclass == PTHISPROC
  1443.                     && (parstate >= INDATA
  1444.                         || procclass == CLMAIN)
  1445.                     && !xretslot[type]) {
  1446.                 xretslot[type] = autovar(ONEOF(type,
  1447.                     MSKCOMPLEX|MSKCHAR) ? 0 : 1, type,
  1448.                     v->vleng, " ret_val");
  1449.                 if (procclass == CLMAIN)
  1450.                     errstr(
  1451.                 "illegal use of %.60s (main program name)",
  1452.                     v->fvarname);
  1453.                 /* not completely right, but enough to */
  1454.                 /* avoid memory faults; we won't */
  1455.                 /* emit any C as we have illegal Fortran */
  1456.                 }
  1457.             }
  1458.     }
  1459.     else if(v->vtype!=type) {
  1460.  incompat:
  1461.         dclerr("incompatible type declarations", v);
  1462.         }
  1463.     else if (type==TYCHAR)
  1464.         if (v->vleng && v->vleng->constblock.Const.ci != length)
  1465.             goto incompat;
  1466.         else if (parstate >= INDATA)
  1467.             v->vleng = ICON(1);    /* avoid a memory fault */
  1468. }
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474. /* lengtype -- returns the proper compiler type, given input of Fortran
  1475.    type and length specifier */
  1476.  
  1477.  int
  1478. #ifdef KR_headers
  1479. lengtype(type, len)
  1480.     register int type;
  1481.     ftnint len;
  1482. #else
  1483. lengtype(register int type, ftnint len)
  1484. #endif
  1485. {
  1486.     register int length = (int)len;
  1487.     switch(type)
  1488.     {
  1489.     case TYREAL:
  1490.         if(length == typesize[TYDREAL])
  1491.             return(TYDREAL);
  1492.         if(length == typesize[TYREAL])
  1493.             goto ret;
  1494.         break;
  1495.  
  1496.     case TYCOMPLEX:
  1497.         if(length == typesize[TYDCOMPLEX])
  1498.             return(TYDCOMPLEX);
  1499.         if(length == typesize[TYCOMPLEX])
  1500.             goto ret;
  1501.         break;
  1502.  
  1503.     case TYINT1:
  1504.     case TYSHORT:
  1505.     case TYDREAL:
  1506.     case TYDCOMPLEX:
  1507.     case TYCHAR:
  1508.     case TYLOGICAL1:
  1509.     case TYLOGICAL2:
  1510.     case TYUNKNOWN:
  1511.     case TYSUBR:
  1512.     case TYERROR:
  1513. #ifdef TYQUAD
  1514.     case TYQUAD:
  1515. #endif
  1516.         goto ret;
  1517.  
  1518.     case TYLOGICAL:
  1519.         switch(length) {
  1520.             case 0: return tylog;
  1521.             case 1:    return TYLOGICAL1;
  1522.             case 2: return TYLOGICAL2;
  1523.             case 4: goto ret;
  1524.             }
  1525. #if 0 /*!!??!!*/
  1526.         if(length == typesize[TYLOGICAL])
  1527.             goto ret;
  1528. #endif
  1529.         break;
  1530.  
  1531.     case TYLONG:
  1532.         if(length == 0)
  1533.             return(tyint);
  1534.         if (length == 1)
  1535.             return TYINT1;
  1536.         if(length == typesize[TYSHORT])
  1537.             return(TYSHORT);
  1538. #ifdef TYQUAD
  1539.         if(length == typesize[TYQUAD] && use_tyquad)
  1540.             return(TYQUAD);
  1541. #endif
  1542.         if(length == typesize[TYLONG])
  1543.             goto ret;
  1544.         break;
  1545.     default:
  1546.         badtype("lengtype", type);
  1547.     }
  1548.  
  1549.     if(len != 0)
  1550.         err("incompatible type-length combination");
  1551.  
  1552. ret:
  1553.     return(type);
  1554. }
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560. /* setintr -- Set Intrinsic function */
  1561.  
  1562.  void
  1563. #ifdef KR_headers
  1564. setintr(v)
  1565.     register Namep v;
  1566. #else
  1567. setintr(register Namep v)
  1568. #endif
  1569. {
  1570.     int k;
  1571.  
  1572.     if(v->vstg == STGUNKNOWN)
  1573.         v->vstg = STGINTR;
  1574.     else if(v->vstg!=STGINTR)
  1575.         dclerr("incompatible use of intrinsic function", v);
  1576.     if(v->vclass==CLUNKNOWN)
  1577.         v->vclass = CLPROC;
  1578.     if(v->vprocclass == PUNKNOWN)
  1579.         v->vprocclass = PINTRINSIC;
  1580.     else if(v->vprocclass != PINTRINSIC)
  1581.         dclerr("invalid intrinsic declaration", v);
  1582.     if(k = intrfunct(v->fvarname)) {
  1583.         if ((*(struct Intrpacked *)&k).f4)
  1584.             if (noextflag)
  1585.                 goto unknown;
  1586.             else
  1587.                 dcomplex_seen++;
  1588.         v->vardesc.varno = k;
  1589.         }
  1590.     else {
  1591.  unknown:
  1592.         dclerr("unknown intrinsic function", v);
  1593.         }
  1594. }
  1595.  
  1596.  
  1597.  
  1598. /* setext -- Set External declaration -- assume that unknowns will become
  1599.    procedures */
  1600.  
  1601.  void
  1602. #ifdef KR_headers
  1603. setext(v)
  1604.     register Namep v;
  1605. #else
  1606. setext(register Namep v)
  1607. #endif
  1608. {
  1609.     if(v->vclass == CLUNKNOWN)
  1610.         v->vclass = CLPROC;
  1611.     else if(v->vclass != CLPROC)
  1612.         dclerr("invalid external declaration", v);
  1613.  
  1614.     if(v->vprocclass == PUNKNOWN)
  1615.         v->vprocclass = PEXTERNAL;
  1616.     else if(v->vprocclass != PEXTERNAL)
  1617.         dclerr("invalid external declaration", v);
  1618. } /* setext */
  1619.  
  1620.  
  1621.  
  1622.  
  1623. /* create dimensions block for array variable */
  1624.  
  1625.  void
  1626. #ifdef KR_headers
  1627. setbound(v, nd, dims)
  1628.     register Namep v;
  1629.     int nd;
  1630.     struct Dims dims[];
  1631. #else
  1632. setbound(register Namep v, int nd, struct Dims dims[])
  1633. #endif
  1634. {
  1635.     register expptr q, t;
  1636.     register struct Dimblock *p;
  1637.     int i;
  1638.     extern chainp new_vars;
  1639.     char buf[256];
  1640.  
  1641.     if(v->vclass == CLUNKNOWN)
  1642.         v->vclass = CLVAR;
  1643.     else if(v->vclass != CLVAR)
  1644.     {
  1645.         dclerr("only variables may be arrays", v);
  1646.         return;
  1647.     }
  1648.  
  1649.     v->vdim = p = (struct Dimblock *)
  1650.         ckalloc( sizeof(int) + (3+2*nd)*sizeof(expptr) );
  1651.     p->ndim = nd--;
  1652.     p->nelt = ICON(1);
  1653.     doin_setbound = 1;
  1654.  
  1655.     for(i = 0; i <= nd; ++i)
  1656.     {
  1657.         if( (q = dims[i].ub) == NULL)
  1658.         {
  1659.             if(i == nd)
  1660.             {
  1661.                 frexpr(p->nelt);
  1662.                 p->nelt = NULL;
  1663.             }
  1664.             else
  1665.                 err("only last bound may be asterisk");
  1666.             p->dims[i].dimsize = ICON(1);
  1667.             p->dims[i].dimexpr = NULL;
  1668.         }
  1669.         else
  1670.         {
  1671.  
  1672.             if(dims[i].lb)
  1673.             {
  1674.                 q = mkexpr(OPMINUS, q, cpexpr(dims[i].lb));
  1675.                 q = mkexpr(OPPLUS, q, ICON(1) );
  1676.             }
  1677.             if( ISCONST(q) )
  1678.             {
  1679.                 p->dims[i].dimsize = q;
  1680.                 p->dims[i].dimexpr = (expptr) PNULL;
  1681.             }
  1682.             else {
  1683.                 sprintf(buf, " %s_dim%d", v->fvarname, i+1);
  1684.                 p->dims[i].dimsize = (expptr)
  1685.                     autovar(1, tyint, EXNULL, buf);
  1686.                 p->dims[i].dimexpr = q;
  1687.                 if (i == nd)
  1688.                     v->vlastdim = new_vars;
  1689.                 v->vdimfinish = 1;
  1690.             }
  1691.             if(p->nelt)
  1692.                 p->nelt = mkexpr(OPSTAR, p->nelt,
  1693.                     cpexpr(p->dims[i].dimsize) );
  1694.         }
  1695.     }
  1696.  
  1697.     q = dims[nd].lb;
  1698.     if(q == NULL)
  1699.         q = ICON(1);
  1700.  
  1701.     for(i = nd-1 ; i>=0 ; --i)
  1702.     {
  1703.         t = dims[i].lb;
  1704.         if(t == NULL)
  1705.             t = ICON(1);
  1706.         if(p->dims[i].dimsize)
  1707.             q = mkexpr(OPPLUS, t,
  1708.                 mkexpr(OPSTAR, cpexpr(p->dims[i].dimsize), q));
  1709.     }
  1710.  
  1711.     if( ISCONST(q) )
  1712.     {
  1713.         p->baseoffset = q;
  1714.         p->basexpr = NULL;
  1715.     }
  1716.     else
  1717.     {
  1718.         sprintf(buf, " %s_offset", v->fvarname);
  1719.         p->baseoffset = (expptr) autovar(1, tyint, EXNULL, buf);
  1720.         p->basexpr = q;
  1721.         v->vdimfinish = 1;
  1722.     }
  1723.     doin_setbound = 0;
  1724. }
  1725.  
  1726.  
  1727.  void
  1728. #ifdef KR_headers
  1729. wr_abbrevs(outfile, function_head, vars)
  1730.     FILE *outfile;
  1731.     int function_head;
  1732.     chainp vars;
  1733. #else
  1734. wr_abbrevs(FILE *outfile, int function_head, chainp vars)
  1735. #endif
  1736. {
  1737.     for (; vars; vars = vars -> nextp) {
  1738.     Namep name = (Namep) vars -> datap;
  1739.     if (!name->visused)
  1740.         continue;
  1741.  
  1742.     if (function_head)
  1743.         nice_printf (outfile, "#define ");
  1744.     else
  1745.         nice_printf (outfile, "#undef ");
  1746.     out_name (outfile, name);
  1747.  
  1748.     if (function_head) {
  1749.         Extsym *comm = &extsymtab[name -> vardesc.varno];
  1750.  
  1751.         nice_printf (outfile, " (");
  1752.         extern_out (outfile, comm);
  1753.         nice_printf (outfile, "%d.", comm->curno);
  1754.         nice_printf (outfile, "%s)", name->cvarname);
  1755.     } /* if function_head */
  1756.     nice_printf (outfile, "\n");
  1757.     } /* for */
  1758. } /* wr_abbrevs */
  1759.